package com.suliao.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.suliao.common.annotation.LoginCheck;
import com.suliao.common.annotation.LoginPass;
import com.suliao.common.base.PageFactory;
import com.suliao.common.base.Result;
import com.suliao.common.base.UserContext;
import com.suliao.common.util.SessionUtil;
import com.suliao.home.entity.content.LabelDO;
import com.suliao.home.entity.industry.*;
import com.suliao.home.entity.product.ManufacturerDO;
import com.suliao.home.entity.product.TypeDO;
import com.suliao.home.entity.product.UseDO;
import com.suliao.home.entity.sys.FileDO;
import com.suliao.home.service.content.LabelService;
import com.suliao.home.service.industry.*;
import com.suliao.home.service.product.ManufacturerService;
import com.suliao.home.service.product.TypeService;
import com.suliao.home.service.sys.FileService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

/**
 * @author ：miaowenbin
 * @date ：Created in 2020/12/15 10:40
 * @description：产业展首页
 * @version: 1.0
 */
@Controller
@RequestMapping(value = "/tradeShow")
@LoginPass
public class IndustryController {
    @Reference
    private IndustryTradeShowService industryTradeShowService;

    @Autowired
    private SessionUtil sessionUtil;

    @Reference
    private TopicService topicService;

    @Reference
    private FileService fileService;

    @Reference
    private LabelService labelService;

    @Reference
    private ApplicationClassService applicationClassService;

    @Reference
    private TypeService typeService;

    @Reference
    private BrandService brandService;

    @Reference
    private ExhibitService exhibitService;

    @Reference
    private ManufacturerService manufacturerService;

    @Reference
    private ExhibitorService exhibitorService;

    @Reference
    private CompanyMainService companyMainService;

    /**
     * |
     * 产业展
     *
     * @param pageName
     * @return
     */
    @GetMapping("page/industry/{pageName}")
    public String show(Model model, @PathVariable("pageName") String pageName) {
        //调service

        model.addAttribute("", "");
        return pageName;
    }

    /**
     * 产业展首页
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(String banPositionBack,
                             String productTitle) {
        ModelAndView modelAndView = null;
        try {
            modelAndView = new ModelAndView("home/tradeShow");
            modelAndView.addObject("productTitle", productTitle);
            modelAndView.addObject("banPositionBack", banPositionBack);
            // 获取主题专区信息
            List<TopicDO> industryTopicList = industryTradeShowService.listIndustryTopicName();
            modelAndView.addObject("industryTopicList", industryTopicList);

            // 获取应用行业信息
            List<UseDO> industryApplicationList = industryTradeShowService.listApplicationName();
            modelAndView.addObject("industryApplicationList", industryApplicationList);

            // 获取前4条展品信息
            ExhibitDO industryProductDto = new ExhibitDO();
            List<ExhibitDO> industryProductDtoList = industryTradeShowService.listIndustryProductName(industryProductDto);
            List<ExhibitDO> fistPictureList = Lists.newArrayList();
            if (industryProductDtoList.size() > 0) {
                for (int i = 0; i < industryProductDtoList.size(); i++) {
                    String fileId = industryProductDtoList.get(i).getImageId();
                    //根据展品的UUID去查询相应的图片，用于展品展示
                    FileDO file = fileService.getFile(fileId);
                    if (file == null) {
                        continue;
                    }
                    String picUrlStr = file.getFileUrl();
                    if (picUrlStr == null) {
                        continue;
                    }
                    if (picUrlStr.indexOf(";") == -1) {
                        industryProductDtoList.get(i).setImageUrl(picUrlStr);
                        fistPictureList.add(industryProductDtoList.get(i));
                    } else {
                        String[] picOssUrlArr = picUrlStr.split(";");
                        industryProductDtoList.get(i).setImageUrl(picOssUrlArr[0]);
                        fistPictureList.add(industryProductDtoList.get(i));
                    }
                }
            }
            modelAndView.addObject("industryProductDtoList", fistPictureList);
            // 获取后4条展品信息
            List<ExhibitDO> industryProductDtoList1 = industryTradeShowService.listIndustryProduct(industryProductDto);
            List<ExhibitDO> PictureList = Lists.newArrayList();
            if (industryProductDtoList.size() > 0) {
                for (int i = 0; i < industryProductDtoList1.size(); i++) {
                    String fileId = industryProductDtoList1.get(i).getImageId();
                    //根据展品的UUID去查询相应的图片，用于展品展示
                    FileDO fileDO = fileService.getFile(fileId);
                    String picUrlStr = fileDO.getFileUrl();
                    if (picUrlStr.indexOf(";") == -1) {
                        industryProductDtoList1.get(i).setImageUrl(picUrlStr);
                        PictureList.add(industryProductDtoList1.get(i));
                    } else {
                        String[] picOssUrlArr = picUrlStr.split(";");
                        industryProductDtoList1.get(i).setImageUrl(picOssUrlArr[0]);
                        PictureList.add(industryProductDtoList1.get(i));
                    }
                }
            }
            modelAndView.addObject("industryProductDtoList1", PictureList);

            // 获取原料企业展商信息
            List<ExhibitorDO> industryExhibitorList = exhibitorService.listIndustryExhibitor();
            modelAndView.addObject("industryExhibitorList", industryExhibitorList);
            // 获取机械企业展商信息
            List<ExhibitorDO> machineryIndustryExhibitorList = exhibitorService.machineryIndustryExhibitor();
            modelAndView.addObject("machineryIndustryExhibitorList", machineryIndustryExhibitorList);
            // 获取制品企业展商信息
            List<ExhibitorDO> productsIndustryExhibitorList = exhibitorService.productsIndustryExhibitor();
            modelAndView.addObject("productsIndustryExhibitorList", productsIndustryExhibitorList);
            // 获取模具企业展商信息
            List<ExhibitorDO> dieIndustryExhibitorList = exhibitorService.dieIndustryExhibitor();
            modelAndView.addObject("dieIndustryExhibitorList", dieIndustryExhibitorList);
            // 获取再生企业展商信息
            List<ExhibitorDO> regrowtIndustryExhibitorList = exhibitorService.regrowthIndustryExhibitor();
            modelAndView.addObject("regrowtIndustryExhibitorList", regrowtIndustryExhibitorList);
            // 获取标签信息
            List<LabelDO> labelDOList = labelService.getLabelDOList();
            modelAndView.addObject("industryTagsList", labelDOList);

            // 获取公司信息
            CompanyMainDO enterpriseInfoDto = new CompanyMainDO();
            List<CompanyMainDO> enterpriseInfoDtoList = companyMainService.listCopyName(enterpriseInfoDto);
            modelAndView.addObject("enterpriseInfoDtoList", enterpriseInfoDtoList);
            // 获取登录人信息
            UserContext userContext = sessionUtil.getUserContext();
            modelAndView.addObject("userinfo", userContext);
            if (userContext == null) {
                System.out.println("----------");
            } else {
                Integer companyId = userContext.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (companyId == null) {
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                } else {
                   /* enterpriseInfoDto.setUuid(companyId);
                    plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);*/
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                }
            }
            //用于首页样式选中样式回显
            String ProductName = "产业展";
            modelAndView.addObject("ProductName", ProductName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 跳转主题专区页面
     * @param page
     * @param productTitle
     * @param banPositionBack
     * @return
     */
    @RequestMapping(value = "/themeArea", method = RequestMethod.GET)
    public ModelAndView themeArea(Integer page,
                                  String productTitle,
                                  String banPositionBack) {
        ModelAndView modelAndView = null;
        try {
            modelAndView = new ModelAndView("tradeShow/themeArea");
            modelAndView.addObject("productTitle", productTitle);
            modelAndView.addObject("banPositionBack", banPositionBack);
            Integer pageSize = 10;
            if ("".equals(page) || page == null) {
                page = 1;//显示首页
                pageSize = 10;//每页10项
            }
            Page<LabelDO> pageInfo = labelService.pageList(PageFactory.createPageInfo(page, pageSize));

            modelAndView.addObject("IndustryTopicList", pageInfo.getRecords());

            //初始化页面，页码数
            modelAndView.addObject("pageNum", page);
            //获取所有的数据的条数
            modelAndView.addObject("allNum", pageInfo.getTotal());
            modelAndView.addObject("pageS", pageInfo.getSize());
            // 获取标签信息
            List<LabelDO> labelDOList = labelService.getLabelDOList();
            modelAndView.addObject("industryTagsList", labelDOList);
            // 获取登录人信息
            UserContext userContext = sessionUtil.getUserContext();
            modelAndView.addObject("userinfo", userContext);
            if (userContext == null) {
                System.out.println("----------");
            } else {
                Integer companyId = userContext.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (companyId == null) {
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                } else {
                       /* enterpriseInfoDto.setUuid(companyId);
                        plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);*/
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                }
            }

            //用于首页样式选中样式回显
            String ProductName = "主题专区";
            modelAndView.addObject("ProductName", ProductName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 跳转应用行业页面
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "/application", method = RequestMethod.GET)
    public ModelAndView Application(Integer page,
                                    String banPositionBack,
                                    String productTitle) {
        ModelAndView modelAndView = null;
        try {
            modelAndView = new ModelAndView("tradeShow/application");
            modelAndView.addObject("productTitle", productTitle);
            modelAndView.addObject("banPositionBack", banPositionBack);
            Integer pageSize = 10;
            if ("".equals(page) || page == null) {
                page = 1;//显示首页
                pageSize = 20;//每页10项
            }
            Page<ApplicationClassDO> pageInfo = applicationClassService.pageIndustryApplication(PageFactory.createPageInfo(page, pageSize));

            modelAndView.addObject("IndustryApplicationList", pageInfo.getRecords());

            //初始化页面，页码数
            modelAndView.addObject("pageNum", page);
            //获取所有的数据的条数
            modelAndView.addObject("allNum", pageInfo.getTotal());
            modelAndView.addObject("pageS", pageInfo.getSize());

            // 获取标签信息
            List<LabelDO> labelDOList = labelService.getLabelDOList();
            modelAndView.addObject("industryTagsList", labelDOList);

            // 获取登录人信息
            UserContext userContext = sessionUtil.getUserContext();
            modelAndView.addObject("userinfo", userContext);
            if (userContext == null) {
                System.out.println("----------");
            } else {
                Integer companyId = userContext.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (companyId == null) {
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                } else {
                       /* enterpriseInfoDto.setUuid(companyId);
                        plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);*/
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                }
            }
            //用于首页样式选中样式回显
            String ProductName = "行业应用";
            modelAndView.addObject("ProductName", ProductName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 跳转品牌街页面
     * @param productTitle
     * @param banPositionBack
     * @return
     */
    @RequestMapping(value = "/brandStreet", method = RequestMethod.GET)
    public ModelAndView brandStreet(String productTitle, String banPositionBack) {
        ModelAndView modelAndView = null;
        try {
            modelAndView = new ModelAndView("tradeShow/brandStreet");
            modelAndView.addObject("banPositionBack", banPositionBack);
            modelAndView.addObject("productTitle", productTitle);
            // 获取登录人信息
            UserContext userContext = sessionUtil.getUserContext();
            modelAndView.addObject("userinfo", userContext);
            if (userContext == null) {
                System.out.println("----------");
            } else {
                Integer companyId = userContext.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (companyId == null) {
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                } else {
                       /* enterpriseInfoDto.setUuid(companyId);
                        plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);*/
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                }
            }
            //用于首页样式选中样式回显
            String ProductName = "品牌街";
            modelAndView.addObject("ProductName", ProductName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 品牌街局部刷新
     */
    @RequestMapping(value = "/getBrandStreet")
    @ResponseBody
    public Result getBrandStreet(String pageNum, Integer customParentId, String customClassName, String brandInitials) {
        Result result = Result.ok();
        try {
            Integer pageSize = 10;
            Integer pageNumber = Integer.valueOf(pageNum);
            pageNumber = (pageNumber == null || pageNumber == 0) ? 1 : pageNumber;
            Map res = Maps.newHashMap();
            // 面包屑
            List<TypeDO> newList = Lists.newArrayList();
            if (customParentId != null) {
                TypeDO typeDO = typeService.findById(customParentId);
                if (typeDO == null) {
                    res.put("crumbsList", newList);
                } else {
                    int customLevel = typeDO.getLevel();
                    if (customLevel == 1) {
                        newList.add(typeDO);
                    } else if (customLevel == 2) {
                        TypeDO parentCustomDto = typeService.findById(typeDO.getParentId());
                        newList.add(parentCustomDto);
                        newList.add(typeDO);
                    } else if (customLevel == 3) {
                        TypeDO secondParentCustomDto = typeService.findById(typeDO.getParentId());
                        TypeDO firstParentCustomDto = typeService.findById(secondParentCustomDto.getParentId());
                        newList.add(firstParentCustomDto);
                        newList.add(secondParentCustomDto);
                        newList.add(typeDO);
                    }
                }
                res.put("crumbsList", newList);

                // 获取产品分类信息
                List<TypeDO> industryCustomClassDtoList;
                if (customParentId != null) {
                    // 一级分类
                    industryCustomClassDtoList = typeService.customNames();
                } else {
                    industryCustomClassDtoList = typeService.customName(customParentId);
                    if (industryCustomClassDtoList.size() == 0) {
                        industryCustomClassDtoList = typeService.custom(customParentId);
                        for (int i = 0; i < industryCustomClassDtoList.size(); i++) {
                            Integer ParentId = industryCustomClassDtoList.get(i).getParentId();
                            industryCustomClassDtoList = typeService.customName(ParentId);
                        }
                    } else {
                        industryCustomClassDtoList = typeService.customName(customParentId);
                    }
                }
                Integer finalCategoryId = customParentId;
                boolean present = industryCustomClassDtoList.stream().filter(c -> finalCategoryId.equals(c.getId())).findAny().isPresent();
                if (!present) {
                    customParentId = null;
                }
                res.put("industryCustomClassDtoList", industryCustomClassDtoList);

                // 字母
                List<BrandDO> brandInitialsList = brandService.listBrandInitials();
                res.put("brandInitialsList", brandInitialsList);
                // 品牌街信息
                Page<ExhibitDO> industryProductDtoList = exhibitService.listBrandStreet(PageFactory.createPageInfo(pageNumber, pageSize), customClassName, brandInitials);
                res.put("pageList", industryProductDtoList.getRecords());
                // 当前页面总页数
                Long allNum = industryProductDtoList.getTotal();
                res.put("allNum", allNum);

                res.put("pageS", industryProductDtoList.getSize());
                res.put("pageNum", pageNum);
                // 展品图片
                List<ExhibitDO> fistPictureList = Lists.newArrayList();
                if (industryProductDtoList.getRecords().size() > 0) {
                    for (int i = 0; i < industryProductDtoList.getRecords().size(); i++) {
                        String fileId = industryProductDtoList.getRecords().get(i).getImageId();
                        //根据展品的UUID去查询相应的图片，用于展品展示
                        FileDO fileDO = fileService.getFile(fileId);
                        String picUrlStr = fileDO.getFileUrl();
                        if (picUrlStr.indexOf(";") == -1) {
                            industryProductDtoList.getRecords().get(i).setImageUrl(picUrlStr);
                            fistPictureList.add(industryProductDtoList.getRecords().get(i));
                        } else {
                            String[] picOssUrlArr = picUrlStr.split(";");
                            industryProductDtoList.getRecords().get(i).setImageUrl(picOssUrlArr[0]);
                            fistPictureList.add(industryProductDtoList.getRecords().get(i));
                        }
                    }
                }

                res.put("industryProductDtoList", fistPictureList);
                res.put("customParentId", industryCustomClassDtoList.size() == 0 ? "" : customParentId);
                res.put("finalCategoryId", finalCategoryId);
                res.put("brandInitials", brandInitials);
                result.setData(res);
            }
        } catch (Exception e) {
            result = Result.error("系统错误");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 跳转实力厂家页面
     * @param productTitle
     * @param banPositionBack
     * @return
     */
    @RequestMapping(value = "/PowerPlant", method = RequestMethod.GET)
    public ModelAndView PowerPlant(String productTitle, String banPositionBack) {
        ModelAndView modelAndView = null;
        try {
            modelAndView = new ModelAndView("tradeShow/PowerPlant");
            modelAndView.addObject("productTitle", productTitle);
            modelAndView.addObject("banPositionBack", banPositionBack);
            // 获取登录人信息
            UserContext userContext = sessionUtil.getUserContext();
            modelAndView.addObject("userinfo", userContext);
            if (userContext == null) {
                System.out.println("----------");
            } else {
                Integer companyId = userContext.getCompanyId();
                //查询塑金余额
                String plasticGoldList = "0.00";
                if (companyId == null) {
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                } else {
                       /* enterpriseInfoDto.setUuid(companyId);
                        plasticGoldList = industryExhibitorService.findAllPlasticGolds(enterpriseInfoDto);*/
                    modelAndView.addObject("plasticGoldList", plasticGoldList);
                }
            }

            //用于首页样式选中样式回显
            String ProductName = "实力工厂";
            modelAndView.addObject("ProductName", ProductName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 实力工厂局部刷新
     * @param pageNum
     * @param customParentId
     * @param customClassName
     * @return
     */
    @RequestMapping(value = "/getPowerPlant")
    @ResponseBody
    public Result getPowerPlant(String pageNum, Integer customParentId, String customClassName) {
        Result result = Result.ok();
        try {
            Integer pageSize = 10;
            Integer pageNumber = Integer.valueOf(pageNum);
            pageNumber = (pageNumber == null || pageNumber == 0) ? 1 : pageNumber;
            Map res = Maps.newHashMap();
            // 面包屑
            List<TypeDO> newList = Lists.newArrayList();
            if (customParentId != null) {
                TypeDO customClassDto = typeService.findById(customParentId);
                if (customClassDto == null) {
                    res.put("crumbsList", newList);
                } else {
                    int customLevel = customClassDto.getLevel();
                    if (customLevel == 1) {
                        newList.add(customClassDto);
                    } else if (customLevel == 2) {
                        TypeDO parentCustomDto = typeService.getById(customClassDto.getParentId());
                        newList.add(parentCustomDto);
                        newList.add(customClassDto);
                    } else if (customLevel == 3) {
                        TypeDO secondParentCustomDto = typeService.getById(customClassDto.getParentId());
                        TypeDO firstParentCustomDto = typeService.getById(secondParentCustomDto.getParentId());
                        newList.add(firstParentCustomDto);
                        newList.add(secondParentCustomDto);
                        newList.add(customClassDto);
                    }
                }

                res.put("crumbsList", newList);
                // 获取产品分类信息
                List<TypeDO> industryCustomClassDtoList;
                if (customParentId == null) {
                    // 一级分类
                    industryCustomClassDtoList = typeService.customNames();
                } else {
                    industryCustomClassDtoList = typeService.customName(customParentId);
                    if (industryCustomClassDtoList.size() == 0) {
                        industryCustomClassDtoList = typeService.custom(customParentId);
                        if (industryCustomClassDtoList.size() > 0) {
                            for (int i = 0; i < industryCustomClassDtoList.size(); i++) {
                                Integer ParentId = industryCustomClassDtoList.get(i).getParentId();
                                industryCustomClassDtoList = typeService.customName(ParentId);
                            }
                        }

                    } else {
                        industryCustomClassDtoList = typeService.customName(customParentId);
                    }
                }
                Integer finalCategoryId = customParentId;
                boolean present = industryCustomClassDtoList.stream().filter(c -> finalCategoryId.equals(c.getId())).findAny().isPresent();
                if (!present) {
                    customParentId = null;
                }
                // 实力工厂信息
                res.put("industryCustomClassDtoList", industryCustomClassDtoList);
                Page<ManufacturerDO> industryProductDtoList = manufacturerService.listPowerPlant(PageFactory.createPageInfo(pageNumber, pageSize), customClassName);
                res.put("pageList", industryProductDtoList.getRecords());
                // 当前页面总页数
                Long allNum = industryProductDtoList.getTotal();
                res.put("allNum", allNum);

                res.put("pageS", industryProductDtoList.getSize());
                res.put("pageNum", pageNum);
                res.put("customParentId", industryCustomClassDtoList.size() == 0 ? "" : customParentId);
                res.put("finalCategoryId", finalCategoryId);
                result.setData(res);
            }
        } catch (Exception e) {
            result = Result.error("系统出错");
            e.printStackTrace();
        }
        return result;
    }
}
